package v1

import (
	"jmicro/app/jshop/api/internal/controller"
	"jmicro/app/jshop/api/internal/domain/request"
	"time"

	"github.com/gin-gonic/gin"
	gin2 "jmicro/app/pkg/translator/gin"

	"jmicro/jmicro/server/restserver/middlewares"
	"jmicro/pkg/common/core"
	jtime "jmicro/pkg/common/time"
)

func (us *userServer) UpdateUser(ctx *gin.Context) {
	updateForm := request.UpdateUserForm{}
	if err := ctx.ShouldBind(&updateForm); err != nil {
		gin2.HandleValidatorError(ctx, err, controller.Trans)
		return
	}
	userID, _ := ctx.Get(middlewares.KeyUserID)
	userIDInt := uint64(userID.(float64))
	userDTO, err := us.userSrv.Get(ctx, userIDInt)
	if err != nil {
		core.WriteResponse(ctx, err, nil)
		return
	}
	//将前端传递过来的日期格式转换成int
	loc, _ := time.LoadLocation("Local") //local的L必须大写
	birthDay, _ := time.ParseInLocation("2006-01-02", updateForm.Birthday, loc)
	userDTO.NickName = updateForm.Name
	userDTO.Birthday = jtime.Time{birthDay}
	userDTO.Gender = updateForm.Gender
	err = us.userSrv.Updata(ctx, userDTO)
	if err != nil {
		core.WriteResponse(ctx, err, nil)
		return
	}
	core.WriteResponse(ctx, nil, nil)
}
